package com.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.constant.MessageConstant;
import com.shop.entity.QueryPageBean;
import com.shop.entity.Result;
import com.shop.pojo.Permission;
import com.shop.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/permission")
@Api(tags = "权限管理")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @PreAuthorize("hasAuthority('QUERY_PERMISSION')")
    @PostMapping("/pageQuery")
    @ApiOperation("权限分页接口")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Page<Permission> page=permissionService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,page);
    }

    @GetMapping("/findAll")
    @ApiOperation("显示权限下拉列表接口")
    public Result findAll(){
        try {
            List<Permission> list=permissionService.findAll();
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

}
